package com.yyq;

import com.yyq.json.readJson;
import org.junit.Test;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

/**
 * @author yyq
 * @create 2021-11-08 9:34
 *
 *
 */
public class fileOperation {

    //遍历输出文件夹下所有的文件名
    public static void getAllFileName(String path){
        File file=new File(path);
        File[] files = file.listFiles();
        for (int i = 0; i < files.length; i++) {
            if(files[i].isDirectory()){
                getAllFileName(files[i].getPath());
            }
            else {
                System.out.print(files[i].getName()+"后缀---------");
                System.out.println(getFileExtensio(files[i].getName()));

            }

        }
    }
    //处理文件名获取后缀名
    public static String getFileExtensio(String fileName){
        int end = fileName.length();
        int begin=fileName.lastIndexOf(".")+ 1;
        String entension = fileName.substring(begin, end);
        return entension;
    }

    //遍历获取结尾为md的所有文件
    public static void getAllFiles(String path,ArrayList<File> list){
        File file=new File(path);
        File[] files = file.listFiles();
        for (int i = 0; i < files.length; i++) {
            if(files[i].isDirectory()){
                //可以省略不扫描的文件夹
                if(readJson.containPath(files[i].getPath())){
                    System.out.println("不处理"+files[i].getPath()+"路径下的文件");
                    return;
                }
                getAllFiles(files[i].getPath(),list);
            }
            else {
                String fileName = files[i].getName();
                String fileExtensio = getFileExtensio(fileName);
                if(fileExtensio.equals("md")){
                    list.add(files[i]);
                }
            }
        }
    }


    public static void main(String[] args) {
        File file=new File("C:\\Users\\yyq\\Desktop\\无用信息");
        File[] files = file.listFiles();

        for (int i = 0; i < files.length; i++) {
            if(files[i].isDirectory()){
                System.out.println("***");
                System.out.println(files[i].getName()+":"+files[i].listFiles().length);
                System.out.println("***");
            }
            else
                System.out.println(files[i].getName());
        }
    }


    @Test
    public void test(){
        String path="D:\\Markdown";
        //getAllFileName(path);
        ArrayList<File> list=new ArrayList<File>();
        getAllFiles(path,list);
        for (int i = 0; i < list.size(); i++) {
            System.out.println(list.get(i).getName());
        }
    }
    @Test
    public void test1(){
        String path="D:\\Markdown";
        File file=new File(path);
        File[] files = file.listFiles();
        System.out.println(files[0].getName());
        System.out.println(files[0].getName().substring(files[0].getName().lastIndexOf(".")+1, files[0].getName().length()));
    }
}
